﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SuperMap.Data;
using SuperMap.Mapping;
using SuperMap.UI;
using SuperMap.Realspace;
using System.Diagnostics;
using System.Collections;

namespace WindowsFormsApplication8
{
    //储存单元，包含SMID 邻接表 是否到达
    public class Unit
    {
       public int id;
       public SList slist;
       public bool visit;
       public Unit(int ID)
       {
           visit = false;
           id = ID;
          slist = null;
       }
        
        
        
    }
    //表模型
    public class SList
    {
        public SList next;
        public int ID;
        public SList(int id)
        {
            ID = id;
            next = null;

        }
 
    }
    //要素类关系图
   public class Relationship
    {
      public  Unit[] unit; 
       //字典用于连接存储单元序列和要素ID；
       public  Dictionary<int,int> dictionary = new Dictionary<int, int>();
        //构造函数
        public void Create(DatasetVector datasetvector)
        {
            
            int i=0;
            Recordset recordset = datasetvector.GetRecordset(false,CursorType.Static);
            unit=new Unit[recordset.RecordCount];
            recordset.MoveFirst();
           //建立元素ID和单元序列的一一对应
            while (!recordset.IsEOF)
            {
                dictionary.Add(recordset.GetID(), i);
                unit[i]=new Unit(recordset.GetID());
                recordset.MoveNext();
                i++;
            }
            i=0;
            
            recordset.MoveFirst();
            //建立元素邻接关系和单元序列邻接关系的一一对应
            while (!recordset.IsEOF)
            {
                Geometry geometry=recordset.GetGeometry();
                QueryParameter queryparameter = new QueryParameter();
                queryparameter.SpatialQueryMode = SpatialQueryMode.Intersect;
                queryparameter.HasGeometry = true;
                queryparameter.CursorType = CursorType.Static;
                queryparameter.SpatialQueryObject=geometry;
                Recordset Recordset = datasetvector.Query(queryparameter);
                Recordset.MoveFirst();
                while (!Recordset.IsEOF)
                {
                    if (unit[i].slist == null)
                    { unit[i].slist = new SList(Recordset.GetID()); Recordset.MoveNext(); }
                    else
                    {
                        SList first = unit[i].slist;
                        unit[i].slist = new SList(Recordset.GetID());
                        unit[i].slist.next = first;
                        Recordset.MoveNext();

                    } 
                }
                recordset.MoveNext(); i++;
            }

        }
       //通过元素ID查找邻接点的元素ID
        public List<int> Search(int ID)
        {
            List<int> list = new List<int>();
            SList slist = unit[dictionary[ID]].slist;
            if (slist == null)
                return list;
            else
            {
                while (slist != null)
                { list.Add(slist.ID); slist = slist.next; }
                return list;
            }

        }

        
    }
}
